package ldh.im.admin.controller;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.base.pojo.ImDepartment;
import ldh.im.base.pojo.where.ImDepartmentWhere;
import ldh.im.base.service.ImDepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2024-03-21 10:36:16
*/
@Controller
@RequestMapping("imDepartment")
public class ImDepartmentController  {

	@Resource(name = "imDepartmentService")
	private ImDepartmentService imDepartmentService;
	
	/**
	 * 字符串转化成时间
	 */
	@InitBinder  
	protected void initBinder(HttpServletRequest request,  
	                              ServletRequestDataBinder binder) throws Exception {  
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());  
	} 

	@RequiresPermissions("imDepartment:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute ImDepartment imDepartment) throws Exception {
    	Objects.requireNonNull(imDepartment);
    	if (imDepartment.getId() == null) {
			imDepartmentService.insert(imDepartment);
    	} else {
			imDepartmentService.updateById(imDepartment);
    	}
    	return "redirect:/imDepartment/list";
    }

	@RequiresPermissions("imDepartment:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute ImDepartmentWhere imDepartment) throws Exception {
    	Objects.requireNonNull(imDepartment);
    	if (imDepartment.getId() == null) {
			imDepartmentService.insert(imDepartment);
		} else {
			imDepartmentService.updateById(imDepartment);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("imDepartment:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}")
    public String toEdit(@PathVariable Long id, Model model) throws Exception {
		ImDepartment imDepartment = imDepartmentService.getById(id);
    	model.addAttribute("imDepartment", imDepartment);
		model.addAttribute("active", "imDepartment");
        return "imDepartment/imDepartmentEdit";
    }

	@RequiresPermissions("imDepartment:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("active", "imDepartment");
        return "imDepartment/imDepartmentEdit";
    }

	@RequiresPermissions("imDepartment:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	ImDepartment imDepartment = imDepartmentService.getById(id);
    	model.addAttribute("imDepartment", imDepartment);
		model.addAttribute("active", "imDepartment");
        return "imDepartment/imDepartmentView";
    }

	@RequiresPermissions("imDepartment:edit")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	ImDepartment imDepartment = imDepartmentService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", imDepartment)
				.toJson();
    }

	@RequiresPermissions("imDepartment:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute ImDepartmentWhere imDepartmentWhere, Model model) {
		imDepartmentWhere.setOrder("id desc");
		PageResult<ImDepartment> imDepartments = imDepartmentService.findByImDepartmentWhere(imDepartmentWhere);
		model.addAttribute("imDepartments", imDepartments);
		model.addAttribute("active", "imDepartment");
		model.addAttribute("imDepartment", imDepartmentWhere);
		return "imDepartment/imDepartmentList";
	}

	@RequiresPermissions("imDepartment:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute ImDepartmentWhere imDepartmentWhere, Model model) {
		imDepartmentWhere.setOrder("id desc");
		PageResult<ImDepartment> imDepartments = imDepartmentService.findByImDepartmentWhere(imDepartmentWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", imDepartments)
				.toJson();
	}
	
	@RequiresPermissions("imDepartment:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		ImDepartment imDepartment = imDepartmentService.getById(id);
        Objects.requireNonNull(imDepartment);
		imDepartmentService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}